﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CamDo.DTO;
using System.Data.SqlClient;

namespace CamDo.DAO
{
    class GiaoDichDAO
    {
        private Connect connectDTO;

        internal Connect ConnectDTO
        {
          get { return connectDTO; }
          set { connectDTO = value; }
        }
                private GiaoDich giaoDichDTO;

        internal GiaoDich GiaoDichDTO
        {
          get { return giaoDichDTO; }
          set { giaoDichDTO = value; }
        }
        SqlConnection connect;

        public SqlConnection Connect
        {
            get { return connect; }
            set { connect = value; }
        }
        String error = "";

        public String Error
        {
            get { return error; }
            set { error = value; }
        }
        public GiaoDichDAO()
        {
            this.connectDTO = new Connect();
            this.giaoDichDTO = new GiaoDich();
            this.connect = this.connectDTO.GetConnect();
        }
        public bool saveARow()
        {
            int result = 0;
            try
            {
                this.connect.Open();
                String strsql = "insert into GIAO_DICH(MaMH,TenMH,SoLuong,LoaiMH,NhaSX,GiaTriGoc,GiaBan_TienChuoc,TenKH,SoDT,SoCMND,LoaiGD,NguoiGD) values(";
                strsql += "'" + giaoDichDTO.MaMatHang + "','" + giaoDichDTO.TenMatHang + "','" + giaoDichDTO.SoLuong + "','" + giaoDichDTO.LoaiMatHang + "','" + giaoDichDTO.NhaSx + "','" + giaoDichDTO.GiaTriGoc;
                strsql += "','" + giaoDichDTO.GiaBanTienChuoc + "','" + giaoDichDTO.TenKhachHang + "','" + giaoDichDTO.SoDienThoai + "','" + giaoDichDTO.SoCMDN + "','" + giaoDichDTO.LoaiGiaoDich + "','" + giaoDichDTO.NguoiGiaoDich + "')";
                SqlCommand cmd = new SqlCommand(strsql, this.connect);
                result = cmd.ExecuteNonQuery();
                this.connect.Close();
            }
            catch (Exception ex)
            {
                Error = "Error save DATA :" + ex.Message;
            }

            if (result <= 0)
                return false;
            return true;
        }
        public int getMaxGiaoDich()
        {
            int maxId = 0;
            try
            {
                SqlCommand myCommand = this.connect.CreateCommand();

                this.connect.Open();
                myCommand.CommandText = "SELECT MAX(MaGD) FROM GIAO_DICH";
                maxId = Convert.ToInt32(myCommand.ExecuteScalar());
            }
            catch (Exception ex)
            {
                this.error = ex.ToString();
            }
            return maxId;
        }

        
    }
}
